package com.awesome.uidemo.customview.systemview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import com.awesome.common.util.log.ShowLogUtil;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2021/8/1 11:29 上午
 * Description:丝滑滚动
 */
public class ScrollerSmoothView extends androidx.appcompat.widget.AppCompatTextView {

    private Scroller mScroller;

    public ScrollerSmoothView(Context context) {
        super(context);
    }

    public ScrollerSmoothView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public ScrollerSmoothView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        ShowLogUtil.info("mScroller.computeScrollOffset()=" + mScroller.computeScrollOffset());
        if (mScroller.computeScrollOffset()) {
            ((View) getParent()).scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
//            ShowLogUtil.info("mScroller.getCurrX() =" + mScroller.getCurrX() + "," + mScroller.getCurrY());
        }
    }

    public void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int scrollY = getScrollY();
        ShowLogUtil.info("scrollX =" + scrollX + ",scrollY = " + scrollY);
        int deltaX = destX - scrollX;
        int deltaY = destY - scrollY;
        // 沿着X轴平移destX，Y轴平移destY
        mScroller.startScroll(scrollX, scrollY, deltaX, deltaY, 2000);
        invalidate();
    }
} 